package ui.systems;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.Rectangle;

import ui.CheckerBoardCanvas;
import ui.CheckerBoardNewGameButton;
import ui.CheckerBoardQuitButton;
import ui.CheckerBoardWindow;

/**
 * Builds the program GUI.
 * 
 * @author Team Titanium
 */
public class CheckersUIBuilder {
	private final int WIN_HEIGHT = 565;
	private final int BOTTOM_WIN_PAD = 65;
	private final int WIN_WIDTH = 785;

	private final Color BUTTON_RGB = new Color(105, 105, 105);
	private final Color LABEL_TEXT_RGB = new Color(255, 255, 255);

	private CheckerBoardWindow cbw;
	private static CheckerBoardCanvas cbc;
	private CheckerBoardQuitButton cbq;
	private CheckerBoardNewGameButton cbn;
	private Label cbl;

	private GraphicsDevice screen;
	private int screen_height;
	private int screen_width;
	private int window_x;
	private int window_y;

	/**
	 * Builds the base window. This must be built first, before any elements can
	 * be added to it.
	 */
	private void buildBaseWindow() {
		cbw = new CheckerBoardWindow();
		// cbw.setUndecorated(true);
		cbw.setBackground(new Color(38, 38, 38));
		cbw.setLayout(null);
		cbw.setBounds(0, 0, WIN_HEIGHT, WIN_HEIGHT + BOTTOM_WIN_PAD);
		cbw.setLocation(window_x, window_y);
	}

	/**
	 * Builds the checkered pattern.
	 * 
	 * @param bnds
	 *            Bounding rectangle of the parent component.
	 */
	private void buildCheckerBoardPattern(Rectangle bnds) {
		cbc = new CheckerBoardCanvas();
		cbc.setBoard(BoardMoveControl.getBoard());
		int wx_width = (int) bnds.getWidth();
		int wx_height = WIN_HEIGHT; // height without padding

		int wx_sq = Math.min(wx_width, wx_height - 65);
		wx_sq = (int) (wx_sq * 0.94);
		cbc.setSize(wx_sq, wx_sq);

		int x_pos = (wx_width - wx_sq) / 2;
		int y_pos = (wx_height - wx_sq) / 2;
		cbc.setLocation(x_pos, y_pos + 30);
	}

	/**
	 * Adds a text label that explains how to play the game.
	 * 
	 * @param bnds
	 *            Bounding rectangle of the parent component
	 */
	private void buildInstructionsLabel(Rectangle bnds) {
		cbl = new Label(
				"Click a piece then click a square to move the piece to it.",
				Label.LEFT);
		if (cbc != null) {
			// Place label relative to the bottom of the canvas.
			Rectangle cbnds = cbc.getBounds();
			cbl.setBounds((int) (cbnds.getX()),
					(int) (cbnds.getY() + cbnds.getHeight() + 5),
					(int) cbnds.getWidth(), 20);
		}
		cbl.setFont(new Font("Arial", 0, 12));
		cbl.setForeground(LABEL_TEXT_RGB);
	}

	/**
	 * Builds the quit button.
	 * 
	 * @param bnds
	 *            Bounding rectangle of the parent component.
	 */
	private void buildQuitButton(Rectangle bnds) {
		cbq = new CheckerBoardQuitButton("Quit");
		cbq.setBounds((int) (bnds.getWidth() - 165),
				(int) (bnds.getHeight() - 45), 100, 30);
		cbq.setBackground(BUTTON_RGB);
	}

	/**
	 * Builds the new game button.
	 * 
	 * @param bnds
	 *            Bounding rectangle of the parent component.
	 */
	private void buildNewGameButton(Rectangle bnds) {
		cbn = new CheckerBoardNewGameButton("New Game");
		int ng_width = 100;
		int ng_height = 30;

		/*
		 * Builds this relative to the Quit button. Should appear to the left of
		 * it.
		 */
		if (cbq != null) {
			Rectangle qbnds = cbq.getBounds();
			cbn.setBounds((int) (qbnds.getWidth() - 10), (int) qbnds.getY(),
					ng_width, ng_height);
		}
		cbn.setBackground(BUTTON_RGB);
	}

	/**
	 * Builds the visual look of the window.
	 */
	public CheckersUIBuilder() {
		// Information about the user's display.
		//screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		//screen_height = screen.getDisplayMode().getHeight();
		//screen_width = screen.getDisplayMode().getWidth();

		// position window in the middle of the screen.
		// window_x = (screen_width-WIN_WIDTH)/2;
		// window_y = (screen_height-WIN_HEIGHT)/2;
		window_x = 150;
		window_y = 0;

		// BASE WINDOW.
		buildBaseWindow();

		// CHECKER BOARD.
		buildCheckerBoardPattern(cbw.getBounds());
		cbw.add(cbc);

		// INSTRUCTIONS LABEL.
		buildInstructionsLabel(cbw.getBounds());
		cbw.add(cbl);

		// QUIT BUTTON.
		buildQuitButton(cbw.getBounds());
		cbw.add(cbq);

		// NEW GAME BUTTON
		buildNewGameButton(cbw.getBounds());
		cbw.add(cbn);
	}

	/**
	 * Play checkers!
	 */
	public void playCheckers() {
		cbw.setVisible(true);
	}

	/**
	 * Return a reference to the board canvas.
	 * 
	 * @return The board canvas.
	 */
	public static CheckerBoardCanvas getBoardCanvas() {
		return cbc;
	}
}
